﻿using System.Text.Json;
using System.Web;
using WeskyQuantDemo.Models;

namespace WeskyQuantDemo.Services
{
    public class QuantDataService: IQuantDataService
    {
        private readonly IHttpClientFactory _httpClientFactory;
        public QuantDataService(IHttpClientFactory httpClientFactory) {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<T> GetStockInfosAsync<T>(string apiUrl)
        {
            try
            {
                using (var client = _httpClientFactory.CreateClient())
                {
                    var response = await client.GetAsync(apiUrl);
                    response.EnsureSuccessStatusCode(); 
                    var content = await response.Content.ReadAsStringAsync();
                    var options = new JsonSerializerOptions
                    {
                        PropertyNameCaseInsensitive = true
                    };
                    return JsonSerializer.Deserialize<T>(content, options);
                }
            }
            catch (HttpRequestException e)
            {
                throw new ApplicationException("Error fetching stock info data", e);
            }
            catch (JsonException e)
            {
                throw new ApplicationException("Error parsing stock info data", e);
            }
        }

        public async Task<string> PostFormDataAsync(string apiUrl, Dictionary<string, string> formData)
        {
            try
            {
                using (var client = _httpClientFactory.CreateClient())
                {
                    var content = new FormUrlEncodedContent(formData);

                    var response = await client.PostAsync(apiUrl, content);
                    response.EnsureSuccessStatusCode(); 

                    return await response.Content.ReadAsStringAsync();
                }
            }
            catch (HttpRequestException e)
            {
                throw new ApplicationException("Error posting form data", e);
            }
        }

        public async Task<string> GetWithQueryParamsAsync(string baseUrl, Dictionary<string, string> queryParams)
        {
            try
            {
                using (var client = _httpClientFactory.CreateClient())
                {
                    var query = HttpUtility.ParseQueryString(string.Empty);
                    foreach (var param in queryParams)
                    {
                        query[param.Key] = param.Value;
                    }
                    var uriBuilder = new UriBuilder(baseUrl) { Query = query.ToString() };
                    string urlWithParams = uriBuilder.ToString();

                    var response = await client.GetAsync(urlWithParams);
                    response.EnsureSuccessStatusCode(); 

                    return await response.Content.ReadAsStringAsync();
                }
            }
            catch (HttpRequestException e)
            {
                throw new ApplicationException("Error fetching data with GET request", e);
            }
        }


    }
}
